import { getTask, postTask, getTaskByID, deleteTaskByID,getUser, 
getTaskCategory, 
getTaskLevel, 
getBlog, 
  } from "@/api/ApiRequest" 

export default {
  data() {
    return {
      /// relateList
      userList: [], 
      taskCategoryList: [], 
      taskLevelList: [], 
      blogList: [], 

      /// table
      columns: [
        {
          title: '记录id',//记录id
          dataIndex: 'id',
          key: 'id',
          sort: 'up',
          align: 'center',
          width: 100,
          showTime: false,
        },
        {
          title: '任务标题',//任务标题
          dataIndex: 'title',
          key: 'title',
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          title: '任务描述',//任务描述
          dataIndex: 'des',
          key: 'des',
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          title: '开始时间',//开始时间
          dataIndex: 'beginTime',
          key: 'beginTime',
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: true,
        },
        {
          title: '持续时间(分钟)',//持续时间(分钟)
          dataIndex: 'duration',
          key: 'duration',
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          title: '分数',//分数
          dataIndex: 'score',
          key: 'score',
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          title: '是否重复 0.否 1.是',//是否重复 0.否 1.是
          dataIndex: 'isRepeat',
          key: 'isRepeat',
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          title: '标签',//标签
          dataIndex: 'tag',
          key: 'tag',
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          title: '关联用户#user',//关联用户#user
          dataIndex: 'userId',
          key: 'userId',
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          title: '关联任务分类#task_category',//关联任务分类#task_category
          dataIndex: 'taskCategoryId',
          key: 'taskCategoryId',
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          title: '关联任务等级#task_level',//关联任务等级#task_level
          dataIndex: 'taskLevelId',
          key: 'taskLevelId',
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          title: '关联学习#blog',//关联学习#blog
          dataIndex: 'learnId',
          key: 'learnId',
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          title: '创建于',//创建于
          dataIndex: 'createAt',
          key: 'createAt',
          sort: 'up',
          align: 'center',
          width: 100,
          showTime: true,
        },
        {
          title: '更新于',//更新于
          dataIndex: 'updateAt',
          key: 'updateAt',
          sort: 'up',
          align: 'center',
          width: 100,
          showTime: true,
        },
        {
          title: '删除于',//删除于
          dataIndex: 'deleteAt',
          key: 'deleteAt',
          sort: 'up',
          align: 'center',
          width: 100,
          showTime: true,
        },

        {
          title: '操作',
          scopedSlots: {
            customRender: 'action',
          },
        },
      ],

      /// 搜索内容
      searchList: [
        {
          name: 'title', //任务标题 
          title: '任务标题',
          type: 'text', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'title',
            {
              rules: [
                { required: 0, message: '任务标题 为必填项' },
              ],
            },
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'des', //任务描述 
          title: '任务描述',
          type: 'text', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'des',
            {
              rules: [
                { required: 0, message: '任务描述 为必填项' },
              ],
            },
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'beginTime', //开始时间 
          title: '开始时间',
          type: 'date', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'beginTime',
            {
              rules: [
                { required: 0, message: '开始时间 为必填项' },
              ],
            },
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: true,
        },
        {
          name: 'duration', //持续时间(分钟) 
          title: '持续时间(分钟)',
          type: 'number', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'duration',
            {
              rules: [
                { required: 0, message: '持续时间(分钟) 为必填项' },
              ],
            },
          ],
          precision: 0,
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'score', //分数 
          title: '分数',
          type: 'number', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'score',
            {
              rules: [
                { required: 0, message: '分数 为必填项' },
              ],
            },
          ],
          precision: 0,
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'isRepeat', //是否重复 0.否 1.是 
          title: '是否重复 0.否 1.是',
          type: 'number', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'isRepeat',
            {
              rules: [
                { required: 0, message: '是否重复 0.否 1.是 为必填项' },
              ],
            },
          ],
          precision: 0,
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'tag', //标签 
          title: '标签',
          type: 'text', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'tag',
            {
              rules: [
                { required: 0, message: '标签 为必填项' },
              ],
            },
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'userId', //关联用户 
          title: '关联用户',
          type: 'select', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'userId',
            {
              rules: [
                { required: 0, message: '关联用户 为必填项' },
              ],
            },
          ],
          options: [
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'taskCategoryId', //关联任务分类 
          title: '关联任务分类',
          type: 'select', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'taskCategoryId',
            {
              rules: [
                { required: 0, message: '关联任务分类 为必填项' },
              ],
            },
          ],
          options: [
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'taskLevelId', //关联任务等级 
          title: '关联任务等级',
          type: 'select', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'taskLevelId',
            {
              rules: [
                { required: 0, message: '关联任务等级 为必填项' },
              ],
            },
          ],
          options: [
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'learnId', //关联学习 
          title: '关联学习',
          type: 'select', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'learnId',
            {
              rules: [
                { required: 0, message: '关联学习 为必填项' },
              ],
            },
          ],
          options: [
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'createAt', //创建于 
          title: '创建于',
          type: 'date', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'createAt',
            {
              rules: [
                { required: 0, message: '创建于 为必填项' },
              ],
            },
          ],
          sort: 'up',
          align: 'center',
          width: 100,
          showTime: true,
        },
        {
          name: 'updateAt', //更新于 
          title: '更新于',
          type: 'date', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'updateAt',
            {
              rules: [
                { required: 0, message: '更新于 为必填项' },
              ],
            },
          ],
          sort: 'up',
          align: 'center',
          width: 100,
          showTime: true,
        },
      ],

      /// 表单信息列表
      formList: [
        {
          name: 'title', //任务标题 
          title: '任务标题',
          type: 'text', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'title',
            {
              rules: [
                { required: 0, message: '任务标题 为必填项' },
              ],
            },
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'des', //任务描述 
          title: '任务描述',
          type: 'text', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'des',
            {
              rules: [
                { required: 0, message: '任务描述 为必填项' },
              ],
            },
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'beginTime', //开始时间 
          title: '开始时间',
          type: 'date', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'beginTime',
            {
              rules: [
                { required: 0, message: '开始时间 为必填项' },
              ],
            },
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: true,
        },
        {
          name: 'duration', //持续时间(分钟) 
          title: '持续时间(分钟)',
          type: 'number', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'duration',
            {
              rules: [
                { required: 0, message: '持续时间(分钟) 为必填项' },
              ],
            },
          ],
          precision: 0,
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'score', //分数 
          title: '分数',
          type: 'number', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'score',
            {
              rules: [
                { required: 0, message: '分数 为必填项' },
              ],
            },
          ],
          precision: 0,
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'isRepeat', //是否重复 0.否 1.是 
          title: '是否重复 0.否 1.是',
          type: 'number', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'isRepeat',
            {
              rules: [
                { required: 0, message: '是否重复 0.否 1.是 为必填项' },
              ],
            },
          ],
          precision: 0,
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'tag', //标签 
          title: '标签',
          type: 'text', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'tag',
            {
              rules: [
                { required: 0, message: '标签 为必填项' },
              ],
            },
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'userId', //关联用户 
          title: '关联用户',
          type: 'select', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'userId',
            {
              rules: [
                { required: 0, message: '关联用户 为必填项' },
              ],
            },
          ],
          options: [
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'taskCategoryId', //关联任务分类 
          title: '关联任务分类',
          type: 'select', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'taskCategoryId',
            {
              rules: [
                { required: 0, message: '关联任务分类 为必填项' },
              ],
            },
          ],
          options: [
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'taskLevelId', //关联任务等级 
          title: '关联任务等级',
          type: 'select', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'taskLevelId',
            {
              rules: [
                { required: 0, message: '关联任务等级 为必填项' },
              ],
            },
          ],
          options: [
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
        {
          name: 'learnId', //关联学习 
          title: '关联学习',
          type: 'select', // text, number, numberRange, select, date, datetime, dateRange
          decorator: [
            'learnId',
            {
              rules: [
                { required: 0, message: '关联学习 为必填项' },
              ],
            },
          ],
          options: [
          ],
          sort: 'up',
          align: 'left',
          width: 100,
          showTime: false,
        },
      ],

      listRequest: getTask,
      addRequest: postTask,
      editRequest: postTask,
      detailRequest: getTaskByID,
      deleteRequest: deleteTaskByID,
    };
  },
  methods: {
    async init() {
      this.getUserList(); 
      this.getTaskCategoryList(); 
      this.getTaskLevelList(); 
      this.getBlogList(); 
    },
    // 获取列表数据
    async getUserList() { 
      let data = await getUser({ "page": 0, "size": 100000 });
      // 转换select options 数据
      data = data.map(v => {
        return {
          "label": v["name"] != undefined ? v["name"] : v["title"],
          "value": v["id"],
        };
      })
      this.userList = data; 
      // 给formlist里的`userId`.options 重新赋值
      this.formList.map(it => {
        if (it["name"] == "userId") {
          it["options"] = this.userList;
        }
      });
      this.searchList.map(it => {
        if (it["name"] == "userId") {
          it["options"] = this.userList;
        }
      });
    },
    // 获取列表数据
    async getTaskCategoryList() { 
      let data = await getTaskCategory({ "page": 0, "size": 100000 });
      // 转换select options 数据
      data = data.map(v => {
        return {
          "label": v["name"] != undefined ? v["name"] : v["title"],
          "value": v["id"],
        };
      })
      this.taskCategoryList = data; 
      // 给formlist里的`taskCategoryId`.options 重新赋值
      this.formList.map(it => {
        if (it["name"] == "taskCategoryId") {
          it["options"] = this.taskCategoryList;
        }
      });
      this.searchList.map(it => {
        if (it["name"] == "taskCategoryId") {
          it["options"] = this.taskCategoryList;
        }
      });
    },
    // 获取列表数据
    async getTaskLevelList() { 
      let data = await getTaskLevel({ "page": 0, "size": 100000 });
      // 转换select options 数据
      data = data.map(v => {
        return {
          "label": v["name"] != undefined ? v["name"] : v["title"],
          "value": v["id"],
        };
      })
      this.taskLevelList = data; 
      // 给formlist里的`taskLevelId`.options 重新赋值
      this.formList.map(it => {
        if (it["name"] == "taskLevelId") {
          it["options"] = this.taskLevelList;
        }
      });
      this.searchList.map(it => {
        if (it["name"] == "taskLevelId") {
          it["options"] = this.taskLevelList;
        }
      });
    },
    // 获取列表数据
    async getBlogList() { 
      let data = await getBlog({ "page": 0, "size": 100000 });
      // 转换select options 数据
      data = data.map(v => {
        return {
          "label": v["name"] != undefined ? v["name"] : v["title"],
          "value": v["id"],
        };
      })
      this.blogList = data; 
      // 给formlist里的`learnId`.options 重新赋值
      this.formList.map(it => {
        if (it["name"] == "learnId") {
          it["options"] = this.blogList;
        }
      });
      this.searchList.map(it => {
        if (it["name"] == "learnId") {
          it["options"] = this.blogList;
        }
      });
    },
    //===================== 处理请求参数
    // 列表请求参数
    async handleListRequestParams(params) {
      this.log("handleListRequestParams", params);
      return params;
    },
    // 新增或者修改保存参数
    async handleSaveRequestParams(params) {
      this.log("handleSaveRequestParams", params);
      return params;
    },
    // 请求详情接口参数
    async handleDetailRequestParams(params) {
      this.log("handleDetailRequestParams", params);
      return params;
    },
    // 删除列表参数
    async handleDeleteRequestParams(params) {
      this.log("handleDeleteRequestParams", params);
      return params;
    },
    // 处理列表返回数据
    // async handleListData(data) {
    //  this.log("handleListData", data);
    //  return data;
    // },
    //===================== search
    // 准备搜索回调
    onWillSearch(params) {
      this.log("onWillSearch", params);
    },
    // 准备搜索回调
    onWillSearchReqeust(params, pagination) {
      this.log("onWillSearchReqeust", params, pagination);
    },
    // 完成搜索回调
    onDidSearch() {
      this.log("onDidSearch");
    },

    //===================== 列表
    // 准备调用列表接口
    onWillGetList() {
      this.log("onWillGetList");
    },
    // 即将开始请求列表接口
    onWillGetListRequest(params) {
      this.log("onWillGetListRequest", params);
    },
    // 列表请求成功
    onGetListSuccess(data) {
      data.map(d => {
        this.userList.map(t => {
          if (d["userId"] == t.value) {
            d["userId"] = t.label;
          }
        })
      })
      
      data.map(d => {
        this.taskCategoryList.map(t => {
          if (d["taskCategoryId"] == t.value) {
            d["taskCategoryId"] = t.label;
          }
        })
      })
      
      data.map(d => {
        this.taskLevelList.map(t => {
          if (d["taskLevelId"] == t.value) {
            d["taskLevelId"] = t.label;
          }
        })
      })
      
      data.map(d => {
        this.blogList.map(t => {
          if (d["learnId"] == t.value) {
            d["learnId"] = t.label;
          }
        })
      })
      
      this.log("onGetListSuccess", data);
    },
    // 列表请求失败
    onGetListError(error) {
      this.log("onGetListError", error);
    },

    //===================== 新增
    // 准备新增
    onWillAdd() {
      this.log("onWillAdd");
    },
    // 新增成功
    onAddSuccess(data) {
      this.log("onAddSuccess", data);
    },
    // 新增失败
    onAddError(error) {
      this.log("onAddError", error);
    },

    //===================== 更新
    // 准备更新
    onWillEdit(data) {
      this.log("onWillEdit", data);
    },
    // 更新成功
    onEditSuccess(data) {
      this.log("onEditSuccess", data);
    },
    // 更新失败
    onEditError(error) {
      this.log("onEditError", error);
    },

    // 准备调用新增或者修改接口
    onWillSaveReqeust(params) {
      this.log("onWillSaveReqeust", params);
    },

    //===================== 删除
    // 准备删除
    onWillDelete(id) {
      this.log("onWillDelete", id);
    },
    // 即将开始请求删除接口
    onWillDeleteReqeust(params) {
      this.log("onWillDeleteReqeust", params);
    },
    // 删除成功
    onDeleteSuccess(data) {
      this.log("onDeleteSuccess", data);
    },
    // 删除失败
    onDeleteError(error) {
      this.log("onDeleteError", error);
    },

    //===================== 其他
    // form表单前缀内容点击
    onFormPrefixClick(error) {
      this.log("onFormPrefixClick", error);
    },
    // form表单后缀内容点击
    onFormSuffixClick(error) {
      this.log("onFormSuffixClick", error);
    },
    log(title, msg) {
      console.log(`[Task->${title}]`, msg ?? "");
    }
  },
};
